package com.redsoil.house.ui.matter.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.redsoil.house.R;
import com.redsoil.house.base.RootActivity;
import com.redsoil.house.model.bean.RoomFileVO;
import com.redsoil.house.model.bean.TestVO;
import com.redsoil.house.model.http.api.ApiConstants;
import com.redsoil.house.model.http.download.ApiHttpManager;
import com.redsoil.house.model.http.download.DownloadFileInterface;
import com.redsoil.house.ui.matter.adapter.MatterFileAdapter;
import com.redsoil.house.ui.matter.contract.MatterContract;
import com.redsoil.house.ui.matter.contract.MatterFileListContract;
import com.redsoil.house.ui.matter.presenter.MatterFileListPresenter;
import com.redsoil.house.ui.matter.presenter.MatterPresenter;
import com.redsoil.house.ui.property.adapter.PropertyLeaseAdapter;
import com.redsoil.house.utils.FileOpenUtils;
import com.redsoil.house.widget.actionbar.XBaseTopBar;
import com.redsoil.house.widget.actionbar.XBaseTopBarImp;
import com.weidingqiang.rxfiflibrary2.app.AppConstants;
import com.weidingqiang.rxfiflibrary2.utils.LogUtil;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 相关文件列表
 */
public class MatterFileListActivity extends RootActivity<MatterFileListPresenter> implements MatterFileListContract.View,XBaseTopBarImp{

    @BindView(R.id.rv_list)
    RecyclerView rv_list;

    @BindView(R.id.main_topbar)
    XBaseTopBar main_topbar;

    private MatterFileAdapter matterFileAdapter;

    private ArrayList<RoomFileVO.FilesBeanX.FilesBean> datas;

    @Override
    protected int getLayout() {
        return R.layout.activity_matter_file_list;
    }

    @Override
    protected void initEventAndData() {

        datas = getIntent().getParcelableArrayListExtra("datas");

        main_topbar.setxBaseTopBarImp(this);

        initAdapter();
    }

    private void initAdapter(){
//        rv_list.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        rv_list.setLayoutManager(new GridLayoutManager(this,3));

        matterFileAdapter = new MatterFileAdapter(R.layout.matter_file_list_item,datas);

        matterFileAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                RoomFileVO.FilesBeanX.FilesBean filesBean = (RoomFileVO.FilesBeanX.FilesBean)adapter.getItem(position);
//                startActivity(PdfViewActivity.newInstance(getBaseContext(), ApiConstants.STATIC_FILE+filesBean.getFilePath()));
                String[] filepath = filesBean.getFilePath().split("/");
                downloadFile(ApiConstants.STATIC_FILE+filesBean.getFilePath(),
                        AppConstants.RECORD_DOWNLOAD_PATH+filepath[filepath.length-1]);
            }
        });

        rv_list.setAdapter(matterFileAdapter);
    }

    //开始下载
    private void downloadFile(String downloadFilePath, String filePath) {

        ToastUtils.showLongToast("正在打开文件,请稍后..");

        //1.开始下载文件
        ApiHttpManager.getManager().downloadAudioFile(downloadFilePath, filePath, new DownloadFileInterface() {

            @Override
            public void onSuccess(String filePath) {
                FileOpenUtils.openFile(getBaseContext(),filePath);
            }

            @Override
            public void onFailure(String des) {
                //3.文件下载失败
                ToastUtils.showShortToast("网络连接超时");
//                downloadDialog.dismiss();
            }

            @Override
            public void onProgress(long bytesWritten, long totalSize) {
//                downtextview.setText("正在下载 " + bytesWritten + " / " + totalSize);
//                arcProgress.setProgress((int) ((bytesWritten * 100) / totalSize));
            }
        });
    }

    @Override
    public void left() {
        finish();
    }

    @Override
    public void right() {}

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    /**
     * 打开新Activity
     *
     * @param context
     * @return
     */
    public static Intent newInstance(Context context,ArrayList<RoomFileVO.FilesBeanX.FilesBean> Files) {
        Intent intent = new Intent(context, MatterFileListActivity.class);
        intent.putParcelableArrayListExtra("datas",Files);
        return intent;
    }

}
